<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PublicController extends Controller
{

    public function qiye_register(request $request){
        if($_POST){
            if($_POST['email']){
                $map['email'] = $_POST['email'];
                $count = db::table('admin_users')->where($map)->count();
                if($count > 0 ){
                    echo "<script>alert('用户名已被使用!');window.history.go(-1);</script>";
                    exit();
                }
                $data['email'] = $_POST['email'];
                $data['password'] = bcrypt($_POST['password']);
                $data['unit_name'] = $_POST['unit_name'];
                $data['name'] = $_POST['name'];
                $data['org_code'] = $_POST['org_code'];
                $data['is_enabled'] = '2';
                $re = db::table('admin_users')->insert($data);
                if($re){
                    echo "<script>alert('注册信息已提交，请等待后台审核!');window.history.go(-1);</script>";
                    exit();
                }else{
                    echo "<script>alert('系统繁忙!');window.history.go(-1);</script>";
                    exit();
                }
            }
        }
        return view('auth/qiye_register');
    }

    //验证重名
    public function check_name(request $request){
        $map['email'] = $request->email;
        $count = db::table('admin_users')->where($map)->count();
        if($count > 0 ){
            return ['status'=>0,'msg'=>'用户名已被占用'];
        }else{
            return ['status'=>1,'msg'=>'用户名可用'];
        }
    }

    //获取地区下级分类
    public function getArea(request $request){
        if ($request->cid) {
            $res = db::table('area')->where('cid','=',$request->cid)->get();
            echo json_encode($res);
        }
    }
}
